package com.sanda.androidcourse;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanda.androidcourse.chapter03.FirstActivity;
import com.sanda.androidcourse.chapter03.WebViewActivity;
import com.sanda.androidcourse.chapter04.CommonViewActivity;
import com.sanda.androidcourse.chapter05.MenuActivity;
import com.sanda.androidcourse.chapter06.FileStorageActivity;
import com.sanda.androidcourse.chapter07.BroadCastActivity;
import com.sanda.androidcourse.chapter07.MyProviderActivity;
import com.sanda.androidcourse.chapter07.MyServiceActivity;
import com.sanda.androidcourse.chapter07.PhoneContactActivity;
import com.sanda.androidcourse.chapter11.GsonParseActivity;
import com.sanda.androidcourse.chapter11.JsonParseActivity;
import com.sanda.androidcourse.chapter11.NetActivity;
import com.sanda.androidcourse.chapter11.WebJsActivity;
import com.sanda.androidcourse.chapter11.WebLoadHtmlActivity;
import com.sanda.androidcourse.chapter11.WebViewLoadActivity;
import com.sanda.androidcourse.modle.User;
import com.sanda.androidcourse.strategy.UserAdapter;
import com.sanda.androidcourse.strategy.bind.BindActivity;
import com.sanda.androidcourse.strategy.mvp.CalculatorActivity;
import com.sanda.androidcourse.strategy.mvvm.CalculatorMVVMActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    Button chapter3Button;
    Button chapter4Button;
    Button chapter5Button;
    Button chapter6Button;

    ImageView ImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        fetchUserData();
        chapter3Button = (Button) findViewById(R.id.chapter3_button);
        chapter4Button = (Button) findViewById(R.id.chapter4_button);
        chapter5Button = (Button) findViewById(R.id.chapter5_button);
        chapter6Button = (Button) findViewById(R.id.chapter6_button);

        chapter3Button.setOnClickListener(view -> {
            Intent intent = new Intent(MainActivity.this, CalculatorMVVMActivity.class);
            startActivity(intent);
        });

        Glide.with(this).load("url").into(ImageView);

        chapter4Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, CalculatorActivity.class);
                startActivity(intent);
            }
        });

        chapter5Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, BindActivity.class);
                startActivity(intent);
            }
        });
    }
    private RecyclerView recyclerView;
    private UserAdapter userAdapter;
    private void fetchUserData() {
        // 创建 OkHttpClient 实例
        OkHttpClient client = new OkHttpClient();
        // 请求 URL
        String url = "https://jsonplaceholder.typicode.com/users";
        // 创建 Request 对象
        Request request = new Request.Builder().url(url).build();
        // 发送异步请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(() -> Toast.makeText(MainActivity.this, "请求失败:" + e.getMessage(), Toast.LENGTH_SHORT).show());
            }
            @Override
            public void onResponse(Call call, Response response) throws
                    IOException {
                if (response.isSuccessful()) {
                    // 获取 JSON 响应体
                    String jsonResponse = response.body().string();
                    // 使⽤ GSON 解析 JSON
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<User>>(){}.getType();
                    List<User> userList = gson.fromJson(jsonResponse,
                            listType);
                    // 更新 UI
                    runOnUiThread(() -> {
                        userAdapter = new UserAdapter(userList);
                        recyclerView.setAdapter(userAdapter);
                    });
                }
                else {
                    runOnUiThread(() -> Toast.makeText(MainActivity.this, "请求失败,响应码：" + response.code(), Toast.LENGTH_SHORT).show());
                }
            }
        });
    }
}